// pages/function/upload/upload-multi.js
const APP = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    targetId:0,
    isEdit:false,
    pageinfo:{
        title:'',
        content:'',
    },
    //上传附件
    attachmentList:[],
    uploadFileObj:{
        images:[],videos:[],audios:[],documents:[]
    },
    uploadRef:null,
    isSubmitLoading: false, //提交状态
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let _this = this;

    //控制权限
    APP.appauth.setAuthSetting();
    //初始化数据
    if(options.tid){
        this.setData({
            targetId:options.tid,
            isEdit:options.tid>0,
        },function(){
            _this.doInitPageData(null);
        });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  /**
   * 【逻辑区域】
   */
   //加载数据
    doInitPageData(completedcallback){
        let _this = this;

        APP._post('/Form/infoMulti', {
          id: this.data.targetId,
        }, false, function (resData) {
            //执行成功
            //组装组件
            for(let item of resData.attachmentList){
                _this.data.attachmentList.push({
                    fileType:item.fileType,
                    filePath:item.filePath,
                    fileName:item.fileName,
                    fileUrl:item.fileUrl,
                });
            }
            
            _this.setData({
                pageinfo:{
                    title:resData.title,
                    content:resData.content,
                },
                attachmentList:_this.data.attachmentList,
            },function(){
                //显示附件
                _this.doSetUploadFile(_this.data.uploadRef,_this.data.attachmentList);
            });
        }, function (res) {
            //执行失败
        }, function (res) {
            //执行完成
            completedcallback && completedcallback();
        });
    },
   //提交表单
    bindFrmSubmit(eventhandle){
        let _this = this;
        let formvalues = eventhandle.detail.value;

        if (formvalues.title.length < 1) {
            APP.apptool.showToastInfo('请输入反映标题!');
            return;
        }
        if (formvalues.content.length < 1) {
            APP.apptool.showToastInfo('请详细说明时间地点和反映问题的具体内容!');
            return;
        }

        //附件组装
        let tmpFiles = this.data.uploadRef.data.getUploadFiles();
        let fileNames=[],filePaths=[],fileTypes=[];
        for(let file of tmpFiles){
            fileNames.push(file.originalname);
            filePaths.push(file.pathname);
            fileTypes.push(file.type);
        }
                
        //禁用操作
        _this.setData({
            isSubmitLoading: true,
        });
        let tmpUrl = (this.data.isEdit?'/Form/submit':'/Form/submit');
        let postData = {            
            title:formvalues.title,
            content:formvalues.content,
            fileNames:fileNames.join(','),
            filePaths:filePaths.join(','),
            fileTypes:fileTypes.join(','),
        };

        //编辑
        if(this.data.isEdit){
            postData.id = this.data.targetId;
        }

        APP._post(tmpUrl,  postData, false, function (resData,res) {
            //执行成功
            APP.apptool.showToastInfo(res.caMsg, null, 2000, function (res) {
                APP.apptool.navigateBackPage(1, function (targetPage) {
                    //需刷新数据面，必须实现refreshPage()方法
                    // targetPage.refreshPage();
                });
            });
        }, function (res) {
            //执行失败
        }, function (res) {
            //执行完成
            //恢复操作
            _this.setData({
                isSubmitLoading: false,
            });
        });
    },
    //设置上传引用
    bindUploadReffer(ref){
        this.data.uploadRef = ref.detail;
    },
    //获取上传附件
    doGetUploadFile(){
        if(this.data.uploadRef){
           let tmpFiles = this.data.uploadRef.data.getUploadFiles();
           return tmpFiles;
        }
        return [];
    },
   //加载上传附件（一）
   doSetUploadFile(ref,attachmentList){
        if(ref){
            ref.data.setUploadFiles(attachmentList);
        }
    },
     //加载上传附件（二）
   doSetUploadFileObj(ref,fileObj){
        if(ref){
            ref.data.setUploadFileObj(fileObj);
        }
    },
    //获取分组附
    doGetGroupAttachment(list){
        let listItemStruct = {
            fileObj:{ images:[],videos:[],audios:[],documents:[]},
            dataType:0,
        }
        for(let item of list){
            switch(item.fileType){
                case '1':
                    listItemStruct.fileObj.images.push({
                        originalname:item.fileName,
                        urlname:APP.appconfig.fwrootweb + item.filePath,
                        pathname:item.filePath,
                        newname:item.fileName,
                        type:1,
                    });
                    break;
                case '2':
                    listItemStruct.fileObj.videos.push({
                        originalname:item.fileName,
                        urlname:APP.appconfig.fwrootweb + item.filePath,
                        pathname:item.filePath,
                        newname:item.fileName,
                        type:1,
                    });
                    break;
                case '3':
                    listItemStruct.fileObj.documents.push({
                        originalname:item.fileName,
                        urlname:APP.appconfig.fwrootweb + item.filePath,
                        pathname:item.filePath,
                        newname:item.fileName,
                        type:3,
                    });
                    break;
            }
        }
        return listItemStruct;
    },

})